<template>
  <div class="container">
    <div class="con-area">
      <!-- 头像 -->
      <div class="img-box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 表单 -->
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm">
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" prefix-icon="iconfont iconyonghu"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" prefix-icon="iconfont iconicon-test"></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login } from '@/api/login'
import { mapActions,mapMutations,mapState } from "vuex";
export default {
  data(){
    return{
      // 表单信息
      ruleForm:{
        username:"admin",
        password:"123456"
      },
      // 验证规则
      rules:{
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        
      }
    }
  },
  computed:{
    ...mapState(["userInfo"])
  },
   methods:{
    ...mapActions(["SaveUserInfo"]),
    ...mapMutations(["set_userinfo"]),
    //  表单验证
    submitForm(formName) {
      const{ruleForm,$api:{ SetToken,Success},set_userinfo}=this
      this.$refs[formName].validate((valid) => {
        console.log(valid)
        if (valid) {
          login({username:ruleForm.username,password:ruleForm.password}).then(res=>{
            console.log(res)
            if(res.data.meta.status===200){
              Success(res.data.meta.msg)
            // 存缓存
            set_userinfo(res.data.data.token)
            // this.SaveUserInfo(res.data.data.token)
            this.$router.push("/home")
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style Lang="less" scoped>
.container{
  background-color: #2b4b6b;
  height: 100%;
}
.con-area{
  width: 450px;
  height: 300px;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  transform: translate(-50%,-50%);
  left: 50%;
  background-color: #fff;
}
.img-box{
  width: 130px;
  height: 130px;
  border-radius: 50%;
  padding: 10px;
  border: 1px solid #f8f8f8;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: #fff;
}
.img-box img{
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.demo-ruleForm{
  padding: 0 20px;
  position: absolute;
  width: 100%;
  box-sizing: border-box;
  bottom: 0;
}
.btn{
  display: flex;
  justify-content: flex-end;
}
</style>